package com.elective.courseselect.controller;

import com.alibaba.fastjson.JSONObject;
import com.elective.courseselect.dao.UserDao;
import com.elective.courseselect.entity.UserEntity;
import com.elective.courseselect.entity.UserVo;
import com.elective.courseselect.service.UserService;
import com.elective.courseselect.utils.PageUtils;
import com.elective.courseselect.utils.R;
import com.elective.status.StatusCode;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;
 /**
 * 
 *
 * @author lee
 * @email 943204006@qq.ccom
 * @date 2021-09-04 19:22:34
 */
@RestController
@RequestMapping("courseselect/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private UserDao userDao;

    /**
     * 列表
     */
    @ApiOperation(value="用户列表" , notes = "后续可支持分页查询")
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @ApiOperation(value="查询单个用户" , notes = "")
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		UserEntity user = userService.getById(id);
        return R.ok().put("user", user);
    }

    /**
     * insert
     */
    @ApiOperation(value="新增用户" , notes = "")
    @PostMapping("/insert")
    public R insertUser(@RequestBody UserEntity user){
        userService.insertUser(user);
        return R.ok(StatusCode.OK,"成功增加用户").put("userId", user.getId());
    }



    /**
     * 修改
     */
    @ApiOperation("7.1.4. 修改个人信息")
    @PostMapping("/update")
    public R update(@RequestBody UserEntity user){
		userService.updateById(user);
        return R.ok(StatusCode.OK,"修改用户密码成功");
    }

    /**
     * 删除
     */
    @ApiOperation(value="删除用户" , notes = "")
    @PostMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		userService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

     @ApiOperation(value="登录" , notes = "")
     @PostMapping("/login")
     public R login(@RequestBody UserEntity user) {
         return userService.login(user);
     }


     @ApiOperation(value="查询学生信息" , notes = "")
     @GetMapping("/searchUserInfos/{userId}")
     public R searchUserInfos(@PathVariable Integer userId) {
         UserVo  userVo = userService.searchUserInfos(userId);
         return R.ok(StatusCode.OK, "查询学生信息成功").put("userVo", userVo);
     }


     @ApiOperation(value="当前最大的学生和老师账号" , notes = "")
     @GetMapping("/getMaxStuAndTeaAccount")
     public R getMaxStuAndTeaAccount() {
         JSONObject userVo = userService.getMaxStuAndTeaAccount();
         return R.ok(StatusCode.OK, "当前最大的学生和老师账号是").put("userVo", userVo);
     }


     @ApiOperation(value="创建账号" , notes = "")
     @PostMapping("/createAccount")
     public R createAccount(@RequestBody JSONObject json) {
         return userService.createAccount(json);
     }

     @ApiOperation(value="删除账号" , notes = "")
     @PostMapping("/delAccount")
     public R delAccount(@RequestBody Integer[] userIds) {
         userService.delAccount(userIds);
         return R.ok(StatusCode.OK, "删除账号成功");
     }




 }
